package br.com.j7.jdesk.persistence.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import br.com.j7.jdesk.persistence.listener.ServiceTypeListener;

/**
 * Entidade ServiceType
 * 
 * @author Edivando Alves
 *
 */
@Entity
@EntityListeners({ServiceTypeListener.class})
@NamedQueries( { @NamedQuery(name="serviceType", 
						query="Select st from ServicesTypes st ORDER BY st.idServiceType"), 
				 @NamedQuery(name="serviceTypeByEnterprise", 
				 		query="from ServicesTypes where enterprise = ?1 ORDER BY idServiceType"), 
				 @NamedQuery(name="serviceTypeByCdServiceTypeAndEnterprise", 
				 		query="from ServicesTypes where cdServiceType = ?1 AND enterprise = ?2 ORDER BY idServiceType"),
				 @NamedQuery(name="serviceTypeByIdServiceTypePaiAndEnterprise",
						 query="from ServicesTypes where idServiceTypePai = ?1 AND enterprise = ?2 ORDER BY idServiceType") } )
public class ServicesTypes implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idServiceType;
	
	private Integer idServiceTypePai;
	
	@JoinColumn(name="idServiceType")
	@OneToMany(mappedBy="serviceType", cascade=CascadeType.REMOVE)
	private List<Services> services;
	
	@Column(nullable=true, length=60, unique=true)
	private String cdServiceType;
	
	@Lob
	@Column(nullable=true)
	private String dsServiceType;
	
	@ManyToOne
	@JoinColumn(name="idEnterprise")
	private Enterprises enterprise;
	
	@Column( length=10 )
	private String cdTimeAttendance;
	
	@Column( length=10 )
	private String cdTimeSolution;
	
	private String dsPathIcon;
	
	private String dsPathItem;
	
	@Version
	private Integer nrVersion;
	
	public ServicesTypes() {
	}
	
	public ServicesTypes(Integer idServiceType){
		this.idServiceType = idServiceType;
	}

	public Integer getIdServiceType() {
		return idServiceType;
	}
	
	public void setIdServiceTypePai(Integer idServiceTypePai) {
		this.idServiceTypePai = idServiceTypePai;
	}
	
	public Integer getIdServiceTypePai() {
		return idServiceTypePai;
	}
	
	public List<Services> getServices() {
		if (services == null) {
			services = new ArrayList<>();
		}
		return services;
	}
	
	public void setServices(List<Services> services) {
		this.services = services;
	}

	public void setIdServiceType(Integer idServiceType) {
		this.idServiceType = idServiceType;
	}

	public String getCdServiceType() {
		return cdServiceType;
	}

	public void setCdServiceType(String cdServiceType) {
		this.cdServiceType = cdServiceType;
	}

	public String getDsServiceType() {
		return dsServiceType;
	}

	public void setDsServiceType(String dsServiceType) {
		this.dsServiceType = dsServiceType;
	}
	
	public Integer getNrVersion() {
		return nrVersion;
	}
	
	public void setNrVersion(Integer nrVersion) {
		this.nrVersion = nrVersion;
	}
	
	public Enterprises getEnterprise() {
		return enterprise;
	}
	
	public void setEnterprise(Enterprises enterprise) {
		this.enterprise = enterprise;
	}
	
	public String getDsPathIcon() {
		return dsPathIcon;
	}
	
	public void setDsPathIcon(String dsPathIcon) {
		this.dsPathIcon = dsPathIcon;
	}
	
	public ServicesTypes addService(Services service){
		service.setServiceType(this);
		getServices().add(service);
		return this;
	}
	
	public void removeService(Services service){
		getServices().remove(service);
	}
	
	public String getCdTimeAttendance() {
		return cdTimeAttendance;
	}

	public void setCdTimeAttendance(String cdTimeAttendance) {
		this.cdTimeAttendance = cdTimeAttendance;
	}

	public String getCdTimeSolution() {
		return cdTimeSolution;
	}

	public void setCdTimeSolution(String cdTimeSolution) {
		this.cdTimeSolution = cdTimeSolution;
	}

	public String getDsPathItem() {
		return dsPathItem;
	}
	
	public void setDsPathItem(String dsPathItem) {
		this.dsPathItem = dsPathItem;
	}
	
	@Override
	public int hashCode() {
		return idServiceType;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ServicesTypes other = (ServicesTypes) obj;
		if (idServiceType == null) {
			if (other.idServiceType != null)
				return false;
		} else if (!idServiceType.equals(other.idServiceType))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return cdServiceType;
	}
}
